﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Artemis;

namespace JAWWS
{
    class Projectile : Actor
    {
        private const float PROJ_STEP = 0.5f;
        private Ball ball;
        public float[] loc;
        private float[] pAt;
        private float[] delta;
        public bool fired;
        private ItemName projType;

        public Projectile(MainForm main, Player player)
        {
            loc = new float[3];
            delta = new float[3];
            pAt = new float[3];

            fired = false;
            this.child = null;
            this.sibling = null;
            this.ball = new Ball(main);
            this.LocalTransMatrix = this.IdMatrix;

            for (int i = 0; i < delta.Length; i++)
            {
                this.delta[i] = 0.0f;
            }
        }
        public void fireProj(Player player, ItemName projType)
        {
            this.projType = projType;
            fired = true;
            Camera camera = player.getCamera();
            camera.getAt().CopyTo(pAt, 0);
            camera.getCam().CopyTo(Position, 0);

            delta[0] = (pAt[0] - Position[0]) * PROJ_STEP;
            delta[1] = (pAt[1] - Position[1]) * PROJ_STEP;
            delta[2] = (pAt[2] - Position[2]) * PROJ_STEP;

            Position[0] = (pAt[0] + camera.getCam()[0]) / 2.0f;
            Position[1] = (pAt[1] + camera.getCam()[1]) / 2.0f;
            Position[2] = (pAt[2] + camera.getCam()[2]) / 2.0f;

            float[] along = new float[3] { 0.0f, 0.0f, 0.0f };
            float[] perp = new float[3] { 0.0f, 0.0f, 0.0f };
            float along_scale = .15f;
            float perp_scale = .5f;
            along[0] = (pAt[0] - Position[0]);
            along[1] = (pAt[1] - Position[1]);
            along[2] = (pAt[2] - Position[2]);

            perp[0] = -(pAt[2] - Position[2]);
            perp[1] = 0;
            perp[2] = (pAt[0] - Position[0]);

            Position[0] = Position[0] + along_scale * along[0] + perp_scale * perp[0];
            Position[1] = Position[1] + along_scale * along[1] + perp_scale * perp[1];
            Position[2] = Position[2] + along_scale * along[2] + perp_scale * perp[2];

            Position[1] = Position[1] - .2f;
            updateProjectile();
        }
        public void moveProj()
        {
            if (fired)
            {
                Position[0] = Position[0] + delta[0];
                Position[1] = Position[1] + delta[1];
                Position[2] = Position[2] + delta[2];
                Position[0] = Position[0] + delta[0];
                Position[1] = Position[1] + delta[1];
                Position[2] = Position[2] + delta[2];
                updateProjectile();
            }
        }
        public void updateProjectile()
        {
            GL.glPushMatrix();
                GL.glLoadIdentity();
                GL.glTranslatef((float)Position.GetValue(0), (float)Position.GetValue(1), (float)Position.GetValue(2));
                GL.glGetFloatv(GLenum.GL_MODELVIEW_MATRIX, this.LocalTransMatrix);
            GL.glPopMatrix();
        }
        public override void draw()
        {
            if (fired)
            {
                if (this.projType == ItemName.RIFLE)
                {
                    Toolkit.SolidTeapot(.05f);
                }
                if (this.projType == ItemName.HANDGUN)
                {
                    Toolkit.SolidSphere(.05f, 10, 10);
                }
            }
        }
        public override void attack(Actor player)
        {
            //does nothing
        }
        public override void attack(Actor actor, Projectile proj)
        {
            //throw new NotImplementedException();
        }
        public float[] getPos()
        {
            return Position;
        }
    }
}
